﻿// ==============================================================================
// Copyright (c) 2012 Bndy.Net, All rights reserved.
// Created by Bndy on 3/21/2012 9:36:32 PM
//
//                    * * * * * * * * * * * * * 
//                    * MAIL- bndy5337@163.com *       http://www.bndy.net
//                    * Q Q - 81795705 =========================================>
//                    * MSN - bndy5337@msn.com *
//                    * * * * * * * * * * * * * 
//
// ------------------------------------------------------------------------------
// 
// ==============================================================================

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace Net.Bndy.Web.Controls
{ 
    /// <summary>
    /// Pager Class for building the pagaration html string.
    /// </summary>
    public class Pager
    {
        const int PAGE_SHOW = 11;

        private int _totalRecords = 0;
        private int _pageRecords = 10;
        private string _linkUrl = string.Empty;

        public int TotalRecords { get { return _totalRecords; } set { _totalRecords = value; } }
        public int PageRecords { get { return _pageRecords; } set { _pageRecords = value; } }
        public string LinkUrl
        {
            set
            {
                _linkUrl = value;
            }
        }
        private int _currentPage
        {
            get
            {
                string page = System.Web.HttpContext.Current.Request["page"];

                int result = 1;
                int.TryParse(page, out result);

                return result;
            }
        }

        private int _totalPages
        {
            get
            {
                int tmp = _totalRecords / _pageRecords;
                if (_totalRecords % _pageRecords > 0)
                {
                    return tmp + 1;
                }

                return tmp;
            }
        }

        public Pager(int totalRescords)
        {
            _totalRecords = totalRescords;
        }

        public Pager(int totalRescords, int pageRecords)
        {
            _totalRecords = totalRescords;
            _pageRecords = pageRecords;
        }

        public string Render()
        {
            if (_totalPages == 0)
                return "No data found.";

            StringBuilder result = new StringBuilder();
            List<int> needPages = new List<int>();
            int HalfPages = PAGE_SHOW / 2;

            int rangeMin = 1;
            int rangeMax = _totalPages;

            if (_totalPages > PAGE_SHOW)
            {
                rangeMin = (_currentPage - HalfPages) <= 0 ? 1 : (_currentPage - HalfPages);
                rangeMax = (_currentPage + HalfPages) >= _totalPages ? _totalPages : (_currentPage + HalfPages);

                if (rangeMax - rangeMin < PAGE_SHOW)
                {
                    if (_currentPage <= HalfPages)
                    {
                        rangeMax = PAGE_SHOW;
                    }
                    else if (_currentPage >= _totalPages - HalfPages)
                    {
                        rangeMin = _totalPages - PAGE_SHOW + 1;
                    }
                }
            }

            if (rangeMin > 2)
                needPages.Add(-1);

            for (int i = rangeMin; i <= rangeMax; i++)
            {
                needPages.Add(i);
            }

            if (rangeMax < _totalPages - 1)
                needPages.Add(-1);

            if (!needPages.Contains(1))
                needPages.Insert(0, 1);

            if (!needPages.Contains(_totalPages))
                needPages.Add(_totalPages);


            foreach (int i in needPages)
            {
                result.Append(BuildPageEntry(i, true));
            }

            return string.Format("<ul>{0}</ul>", result.ToString());
        }

        private string BuildPageEntry(int pageNum, bool enabled)
        {
            if (enabled && pageNum > 0)
                return string.Format("<li><a href=\"{1}\">{0}</a></li>", pageNum, BuildUrl(pageNum));
            else
                return string.Format("<li>{0}</li>", pageNum > 0 ? pageNum.ToString() : "...");
        }

        private string BuildUrl(int page)
        {
            string result = string.Empty;
            HttpRequest request = System.Web.HttpContext.Current.Request;
            List<string> qs = new List<string>();

            foreach (string s in request.QueryString.AllKeys)
            {
                if (!s.Equals("page", StringComparison.OrdinalIgnoreCase))
                    qs.Add(string.Format("{0}={1}", s, request.QueryString[s]));
            }

            qs.Add("page=" + page);

            result = string.Format("{0}?{1}", _linkUrl,
                string.Join("&", qs.ToArray()));

            return result;

        }
    }

}
